package com.ruoyi.aitu9c.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class U9JSONResultUtils {
	/**
	 * 将U9返回的JSONObject转成{code,msg,data}JSON对象
	 * @param result
	 * @return
	 * @throws Exception
	 */
	public static JSONObject toResult(JSONObject result) throws Exception {
		if(result==null) throw new Exception("传入result为空");
		
		JSONObject ret=new JSONObject();
		ret.put("code", result.getJSONObject("d").getInteger("Code"));
		ret.put("msg", result.getJSONObject("d").getString("Msg"));
		Object data = result.getJSONObject("d").get("Data");
		ret.put("data", data);
//		if(ObjectUtil.isNotNull(data)) {
//			Object json = JSONObject.parse(data.toString());
//			ret.put("data",json);
//		}
		return ret;
	}
	
	public static JSONArray getDataArray(JSONObject result) {
		return result.getJSONArray("data");
	}

	/**
	 * 获取 U9返回值的数据部分, 类型是JSONObject和JSONArray两种
	 * @param result U9返回值
	 * @return
	 */
	public static Object getDataObject(JSONObject result) {
		final String dataStr = result.getString("data");
		Object dataObj = JSON.parse(dataStr);//该方法会自动识别JSONObject和JSONArray
		if (dataObj instanceof JSONObject) {
			final JSONObject dataJSON = (JSONObject)dataObj;
			return dataJSON;
		} else if (dataObj instanceof JSONArray) {
			final JSONArray dataArray = (JSONArray)dataObj;
			return dataArray;
		}
		return null;
	}
}
